package fr.emse.tatiana.filter;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import fr.emse.tatiana.TatianaConstants;

import fr.emse.tatiana.display.BadDisplayFile;
import fr.emse.tatiana.display.DisplayFileReader;
import fr.emse.tatiana.replayable.ReplayableInput;
import fr.emse.tatiana.replayable.ReplayableModel;

public class FilterablePathFactory {
	public static String getFilterablePath(String path) {
		IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path));
		String ret = path;
		if (TatianaConstants.REPLAYABLES_FOLDER_NAME.equals(file.getParent().getName())) {

			try {
				String type = null;

				DisplayFileReader reader = new DisplayFileReader(file.getLocation().toString());
				type = reader.getType();
				if (ReplayableInput.DISPLAY_TYPE.equals(type)) {
					ReplayableInput input = new ReplayableInput(file);
					input.init();
					String filepath = input.getDisplayFilePath();
					File f = new File(filepath);
					IFile workspacefile = ResourcesPlugin.getWorkspace().getRoot()
					.getProject(TatianaConstants.TATIANA_PROJECT_NAME)
					.getFolder(TatianaConstants.CACHE_FOLDER_NAME)
					.getFile(f.getName());
					ret = workspacefile.getFullPath().makeRelative().toString();
				} else if (ReplayableModel.DISPLAY_TYPE.equals(type)) {
					ret = path; // use it directly as input
				}
			} catch (BadDisplayFile e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return ret;
	}
}
